/****************************************************************************
**
** Copyright (C) 2012 Róbert Márki
**
** This file is part of Web Feeds.
**
** Web Feeds is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Web Feeds is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Web Feeds.  If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/

import QtQuick 1.1
import com.nokia.meego 1.0
import com.nokia.extras 1.1
import "UIConstants.js" as UIConstants
import "." as Shared

Shared.CommonPageContents {
    id: root

    property Component header
    property bool contentsEnabled: false
    signal currentRowChanged(int row)
    property int initialRow: 0
    property bool __initialRowSet: false

    onInitialRowChanged: {
        if(!root.__initialRowSet) {
            listView.positionViewAtIndex(root.initialRow, ListView.Beginning);
            listView.currentIndex = root.initialRow;
            root.__initialRowSet = true;
        }
    }

    function indexForRow(row) {
        return visualModel.modelIndex(row);
    }

    function itemForRow(row) {
        return webFeedsApp.pageItemModel.item(indexForRow(row));
    }

    VisualDataModel {
        id: visualModel
        model: webFeedsApp.pageItemModel
        delegate: delegateComponenet
        rootIndex: webFeedsApp.pageItemModel.index(root.rootItem)
    }

    BusyIndicator {
        anchors.centerIn: parent
        running: root.busy
        visible: root.busy
        style: BusyIndicatorStyle {
            size: "large"
        }
    }
    ListView {
        id: listView
        anchors.fill: parent
        visible: !root.busy
        orientation: ListView.Horizontal
        snapMode: ListView.SnapOneItem
        highlightRangeMode: ListView.StrictlyEnforceRange
        boundsBehavior: Flickable.StopAtBounds
        preferredHighlightBegin: 0
        preferredHighlightEnd: 0
        model: visualModel
        clip: true
        Component.onDestruction: listView.model = undefined;
        onCurrentIndexChanged: {
            if(listView.model)
                currentRowChanged(currentIndex);
        }
    }

    Component {
        id: delegateComponenet

        Item {
            id: container
            width: listView.width
            height: listView.height

            ScrollDecorator {
                id: scrollDecorator
                flickableItem: flickable
            }

            Flickable {
                id: flickable
                flickableDirection: Flickable.VerticalFlick
                anchors.fill: parent

                contentWidth: flickableContent.width
                contentHeight: flickableContent.height

                Item {
                    id: flickableContent
                    width: root.width
                    height: headerLoader.height + UIConstants.DEFAULT_MARGIN + conents.height;

                    Loader {
                        id: headerLoader
                        anchors.left: parent.left
                        anchors.right: parent.right
                        anchors.top: parent.top
                        height: headerLoader.item.height
                        sourceComponent: root.header
                        onLoaded: headerLoader.item.model = model;
                    }

                    Item {
                        id: conents                        
                        property bool visibilityEnabled: false
                        anchors.top: headerLoader.bottom
                        anchors.topMargin: UIConstants.DEFAULT_MARGIN
                        anchors.left: parent.left
                        anchors.leftMargin: UIConstants.DEFAULT_MARGIN
                        anchors.right: parent.right
                        anchors.rightMargin: UIConstants.DEFAULT_MARGIN
                        height: (contentText.paintedHeight
                                 + UIConstants.DEFAULT_MARGIN + webPageButton.height
                                 + UIConstants.DEFAULT_MARGIN);
                        visible: root.contentsEnabled && visibilityEnabled
                        clip: true

                        function evaluateVisibility() {
                            if(!conents.visibilityEnabled &&
                                    (container.ListView.isCurrentItem || (listView.currentIndex == index)) &&
                                    !listView.moving) {
                                conents.visibilityEnabled = true;
                            }
                        }

                        Connections {
                            target: listView
                            onCurrentIndexChanged: conents.evaluateVisibility();
                            onMovingChanged: conents.evaluateVisibility();
                        }

                        Label {
                            id: contentText
                            property bool contentsEnabled: false
                            anchors.top: parent.top
                            anchors.left: parent.left
                            anchors.right: parent.right
                            wrapMode: Text.Wrap
                            color: UIConstants.STORY_CONTENTS_COLOR
                            styleColor: "darkorange"
                            font.family: UIConstants.FONT_FAMILY_LIGHT
                            onLinkActivated: Qt.openUrlExternally(link);
                            textFormat: Text.RichText
                            text: visible ? model.contents : ""
                        }

                        Shared.Button {
                            id: webPageButton
                            anchors.top: contentText.bottom
                            anchors.topMargin: UIConstants.DEFAULT_MARGIN
                            anchors.horizontalCenter: parent.horizontalCenter
                            text: qsTr("Open web page")
                            onClicked: Qt.openUrlExternally(model.url);
                        }
                    }
                }
            }
        }
    }
}
